package com.excuse.excusebuilder.view.fragment;

import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.excuse.excusebuilder.R;
import com.excuse.excusebuilder.adapter.CommunityFragmentAdapter;
import com.excuse.excusebuilder.base.BaseFragment;
import com.excuse.excusebuilder.bean.Post;
import com.excuse.excusebuilder.impl.view.CommunityFragmentInterface;
import com.excuse.excusebuilder.presenter.CommunityFramentPresenter;
import com.excuse.excusebuilder.view.activity.AddPostActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by U-nookia on 2017/4/13.
 * 社区界面
 */

public class CommunityFragment extends BaseFragment implements CommunityFragmentInterface {

    @BindView(R.id.add_post_fab)
    FloatingActionButton addPostFab;
    @BindView(R.id.recycler_community)
    RecyclerView recyclerCommunity;

    private CommunityFramentPresenter presenter;

    @Override
    protected void initData() {
        presenter = new CommunityFramentPresenter(getContext(), this);
        initRecyclerView();
    }

    @Override
    public void onResume() {
        super.onResume();
        presenter.getPosts();
    }

    private void initRecyclerView() {
        recyclerCommunity.setLayoutManager(new LinearLayoutManager(getContext()));
    }

    @Override
    public int getFragmentLayout() {
        return R.layout.fragment_community;
    }

    @OnClick(R.id.add_post_fab)
    public void onViewClicked() {
        /**/
        startActivity(new Intent(getContext(), AddPostActivity.class));
    }

    @Override
    public void setAdapter(List<Post> mPosts) {
        CommunityFragmentAdapter adapter = new CommunityFragmentAdapter(mPosts);
        recyclerCommunity.setAdapter(adapter);
    }

    @Override
    public void finish() {
        getActivity().finish();
    }
}
